package com.cmii.sjw.department.control.jsc.service.impl;

import com.cmii.sjw.department.control.jsc.mapper.DistrictEducationMapper;
import com.cmii.sjw.department.control.jsc.model.dto.DistrictMetricDTO;
import com.cmii.sjw.department.control.jsc.service.DistrictEducationService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class DistrictEducationServiceImpl implements DistrictEducationService {

    @Resource
    private DistrictEducationMapper districtEducationMapper;

    @Override
    public Map<String, List<Map<String, String>>> getMetricsByDistrict(String districtName) {
        List<DistrictMetricDTO> flatList = districtEducationMapper.selectMetricsByDistrict(districtName);

        Map<String, List<Map<String, String>>> grouped = flatList.stream()
                .collect(Collectors.groupingBy(
                        item -> mapToGroup(item.getSectionName()),
                        LinkedHashMap::new,
                        Collectors.mapping(item -> {
                            Map<String, String> metric = new HashMap<>();
                            metric.put("metricName", item.getMetricName());
                            metric.put("currentValue", item.getCurrentValue());
                            return metric;
                        }, Collectors.toList())
                ));

        return grouped;
    }

    private String mapToGroup(String sectionName) {
        if (sectionName == null) return "other";

        switch (sectionName.trim()) {
            case "义务教育优质均衡":
                return "yiwu";
            case "高中阶段协调协调平衡":
                return "gaozhong";
            case "学前教育普及普惠":
                return "youer";
            case "特殊教育普惠发展":
                return "teshu";
            default:
                return "other";
        }
    }
}